//
//  AddOfferViewController.m
//  EggSteward
//
//  Created by 王道林 on 16/9/12.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "AddOfferViewController.h"
#import "BaseLabel.h"
#import "BaseTextField.h"
#import "BaseButton.h"
#import "Masonry.h"
#import "DLWNetworkingParser.h"
#import "OfferEggModel.h"
@interface AddOfferViewController ()
@property (nonatomic, strong) BaseTextField *textFieldDate;
@property (nonatomic, strong) BaseTextField *textFieldPlace;
@property (nonatomic, strong) BaseTextField *textFieldPrice;
@property (nonatomic, strong) BaseTextField *textFieldRemark;
@property (nonatomic, strong) BaseButton *buttonAdd;
@property (nonatomic, strong) UIAlertController *alertController;
@end

@implementation AddOfferViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithWhite:0.939 alpha:1.000];
    self.navigationItem.title = _navigationTitle;
    [self createSubView];
    [self addGesture];
}

- (void)createSubView
{
    BaseLabel *lableDate = [[BaseLabel alloc] init];
    lableDate.backgroundColor = [UIColor whiteColor];
    lableDate.text = @"     记录日期";
    [self.view addSubview:lableDate];
    
    BaseLabel *labelPlace = [[BaseLabel alloc] init];
    labelPlace.backgroundColor = [UIColor whiteColor];
    labelPlace.text = @"     所在地区";
    [self.view addSubview:labelPlace];
    
    BaseLabel *lablePrice = [[BaseLabel alloc] init];
    lablePrice.backgroundColor = [UIColor whiteColor];
    lablePrice.text = @"     销售价格(元/斤)";
    [self.view addSubview:lablePrice];
    // 获取现在的日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
   NSString *date = [dateFormatter stringFromDate:[NSDate date]];
    _textFieldDate = [[BaseTextField alloc] init];
    _textFieldDate.backgroundColor = [UIColor whiteColor];
    _textFieldDate.placeholder = date;
    [self.view addSubview:_textFieldDate];
    
    _textFieldPlace = [[BaseTextField alloc] init];
    _textFieldPlace.backgroundColor = [UIColor whiteColor];
    _textFieldPlace.placeholder = @"必填";
    [self.view addSubview:_textFieldPlace];
    
    _textFieldPrice = [[BaseTextField alloc] init];
    _textFieldPrice.backgroundColor = [UIColor whiteColor];
    _textFieldPrice.placeholder = @"必填";
    [self.view addSubview:_textFieldPrice];
    // 修改的时候
    _textFieldDate.text = _model.NoteDate;
    _textFieldPlace.text = _model.Province;
    _textFieldPrice.text = _model.EggSalePrice;
    
    _textFieldRemark = [[BaseTextField alloc] init];
    _textFieldRemark.backgroundColor = [UIColor whiteColor];
    _textFieldRemark.placeholder = @"     备注";
    [self.view addSubview:_textFieldRemark];
    
    _buttonAdd = [BaseButton buttonWithType:UIButtonTypeSystem];
    _buttonAdd.layer.masksToBounds = YES;
    _buttonAdd.layer.cornerRadius = 4;
    [_buttonAdd setTitle:@"添加" forState:UIControlStateNormal];
    [_buttonAdd setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_buttonAdd setBackgroundColor:[UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000]];
    // 添加button的点击事件
    [_buttonAdd addTarget:self action:@selector(addOfferAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_buttonAdd];
    
    // 约束
    [lableDate mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top);
        make.left.mas_equalTo(self.view.mas_left);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.4);
        make.height.mas_equalTo(50);
    }];
    
    [_textFieldDate mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top);
        make.left.mas_equalTo(lableDate.mas_right);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.6);
        make.height.mas_equalTo(50);
    }];

    [labelPlace mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lableDate.mas_bottom).mas_offset(1);
        make.left.mas_equalTo(self.view.mas_left);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.4);
        make.height.mas_equalTo(50);
    }];
    
    [_textFieldPlace mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lableDate.mas_bottom).mas_offset(1);
        make.left.mas_equalTo(labelPlace.mas_right);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.6);
        make.height.mas_equalTo(50);
    }];

    [lablePrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(labelPlace.mas_bottom).mas_offset(1);
        make.left.mas_equalTo(self.view.mas_left);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.4);
        make.height.mas_equalTo(50);
    }];
    
    [_textFieldPrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(labelPlace.mas_bottom).mas_offset(1);
        make.left.mas_equalTo(lablePrice.mas_right);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.6);
        make.height.mas_equalTo(50);
    }];

    [_textFieldRemark mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lablePrice.mas_bottom).mas_offset(5);
        make.left.mas_equalTo(self.view.mas_left);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(1);
        make.height.mas_equalTo(100);
    }];

    [_buttonAdd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textFieldRemark.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(self.view.mas_left).mas_offset(20);
        make.right.mas_equalTo(self.view.mas_right).mas_offset(-20);
        make.height.mas_equalTo(50);
    }];
    
    
}

#pragma mark - 回收键盘
- (void)addGesture
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapAction:)];
    [self.view addGestureRecognizer:tap];
}
- (void)TapAction:(UITapGestureRecognizer *)tap
{
    [_textFieldRemark resignFirstResponder];
    [_textFieldDate resignFirstResponder];
    [_textFieldPrice resignFirstResponder];
    [_textFieldPlace resignFirstResponder];
    
}

#pragma mark - button 的点击事件添加报价数据
- (void)addOfferAction
{
 
    // 创建提示框
    _alertController = [UIAlertController alertControllerWithTitle:@"提示消息" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [_alertController addAction:action];


    if (![_textFieldDate.text isEqualToString:@""] && ![_textFieldPlace.text isEqualToString:@""] && ![_textFieldPrice.text isEqualToString:@""])
    {
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        NSString *ticket = [userDefault valueForKey:@"ticket"];
        NSString *uid = [userDefault valueForKey:@"uid"];
      NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:uid,@"uid", _textFieldPrice.text, @"price", _textFieldPlace.text, @"area", _textFieldRemark.text, @"note", ticket, @"ticket", nil];
    
      [DLWNetworkingParser POSTWithURL:@"http://www.danjiguanjia.com/api/Price/submitEggPrice" par:dic success:^(id responseObject) {
          // 推出提示框
          NSString *string = responseObject[@"msg"];
          _alertController.message = string;
          [self presentViewController:_alertController animated:YES completion:^{
              
          }];
      } filed:^(NSError *error) {
        
      }];
    } else if([_textFieldDate.text isEqualToString:@""])
    {
        // 推出提示框
        _alertController.message = @"日期不能为空";
        [self presentViewController:_alertController animated:YES completion:^{
            
        }];
    } else if ([_textFieldPlace.text isEqualToString:@""])
    {
        // 推出提示框
        _alertController.message = @"地址不能为空";
        [self presentViewController:_alertController animated:YES completion:^{
            
        }];
    }else if ([_textFieldPrice.text isEqualToString:@""])
    {
        // 推出提示框
        _alertController.message = @"价格不能为空";
        [self presentViewController:_alertController animated:YES completion:^{
            
        }];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
